package renthouse.dao;/*
 * @创建者：黄苟苟
 * @创建时间：2021/5/18   18:10
 * @Description描述
 **/


import renthouse.entry.Admin;
import renthouse.util.ConnDb;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;


public class AdminDao {
    
    public boolean insert(Admin admin) {
        String s="insert into admin(name,sex,phone,numberid,loginname,password) values (?,?,?,?,?,?)";
        Connection connection= ConnDb.getConnection();
        PreparedStatement p=null;
        try {
            p=connection.prepareStatement(s);
            p.setString(1, admin.getName());
            p.setString(2,admin.getSex());
            p.setString(3,admin.getPhone());
            p.setString(4,admin.getNumberid());
            p.setString(5,admin.getLoginname());
            p.setString(6,admin.getPassword());

            int i=  p.executeUpdate();
            if (i>0){
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }finally {
            ConnDb.close(connection,p,null);  //关闭
        }

        return false;
    }

   
    public boolean deleteById(int i) {
        String deleteStr="delete from admin where id ="+i;
        Connection connection=ConnDb.getConnection();
        Statement statement=ConnDb.getStatement(connection);
        try {
            int len=statement.executeUpdate(deleteStr);
            if (len>0){
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }finally {
            ConnDb.close(connection,statement,null);
        }
        return false;
    }

   
    public boolean updateById(Admin admin) {
        if (admin.getId()==0){
            return false;
        }
        //拼接sql语句
        String updateStr="update admin set id="+admin.getId();

        if (admin.getName()!=null){
            updateStr+=",name='"+admin.getName()+"'";
        }

        if (admin.getSex()!=null){
            updateStr+=",sex='"+admin.getSex()+"'";
        }

        if (admin.getPhone()!=null){
            updateStr+=",phone='"+admin.getPhone()+"'";
        }

        if (admin.getNumberid()!=null){
            updateStr+=",numberid='"+admin.getNumberid()+"'";
        }

        if (admin.getLoginname()!=null){
            updateStr+=",loginname="+admin.getLoginname();
        }

        if (admin.getPassword()!=null){
            updateStr+=",password="+admin.getPassword();
        }


        //加上条件

        updateStr+=" where id="+admin.getId();


        Connection connection= ConnDb.getConnection();
        Statement statement= ConnDb.getStatement(connection);
        try {
            int len=statement.executeUpdate(updateStr);
            if (len>0){
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }finally {
            ConnDb.close(connection,statement,null);
        }
        return false;
    }

    
    public List <Admin> query(Admin admin) {
        String str="SELECT * FROM admin where 1=1 ";
        List<Admin> list=new ArrayList <>();
        if (admin.getName()!=null){
            str+=" and name like concat('%','"+admin.getName()+"','%')";
        }

        if (admin.getSex()!=null){
            str+=" and sex like concat('%','"+admin.getSex()+"','%')";
        }

        if (admin.getPhone()!=null){
            str+=" and phone like concat('%','"+admin.getPhone()+"','%')";
        }

        if (admin.getNumberid()!=null){
            str+=" and numberid like concat('%','"+admin.getNumberid()+"','%')";
        }

        if (admin.getLoginname()!=null){
            str+=" and loginname like concat('%','"+admin.getLoginname()+"','%')";
        }

        if (admin.getPassword()!=null){
            str+=" and password like concat('%','"+admin.getPassword()+"','%')";
        }


        Connection connection=ConnDb.getConnection();
        Statement statement=ConnDb.getStatement(connection);
        ResultSet resultSet=null;

        try {
            resultSet=statement.executeQuery(str);
            while (resultSet.next()){
                Admin landlord1=new Admin();
                landlord1.setId(resultSet.getInt("id"));
                landlord1.setName(resultSet.getString("name"));
                landlord1.setSex(resultSet.getString("sex"));
                landlord1.setPhone(resultSet.getString("phone"));
                landlord1.setNumberid(resultSet.getString("numberid"));
                landlord1.setLoginname(resultSet.getString("loginname"));
                landlord1.setPassword(resultSet.getString("password"));

                list.add(landlord1);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }


        return list;
    }

    
    public List <Admin> queryForPage(Admin admin, int pageNum, int pageSize) {

        int start = (pageNum - 1) * pageSize;
        String str="SELECT * FROM admin where 1=1 ";
        List<Admin> list=new ArrayList <>();
        if (admin.getName()!=null){
            str+=" and name like concat('%','"+admin.getName()+"','%')";
        }

        if (admin.getSex()!=null){
            str+=" and sex like concat('%','"+admin.getSex()+"','%')";
        }

        if (admin.getPhone()!=null){
            str+=" and phone like concat('%','"+admin.getPhone()+"','%')";
        }

        if (admin.getNumberid()!=null){
            str+=" and numberid like concat('%','"+admin.getNumberid()+"','%')";
        }

        if (admin.getLoginname()!=null){
            str+=" and loginname like concat('%','"+admin.getLoginname()+"','%')";
        }

        if (admin.getPassword()!=null){
            str+=" and password like concat('%','"+admin.getPassword()+"','%')";
        }

        str += " limit " + start + "," + pageSize;
        Connection connection=ConnDb.getConnection();
        Statement statement=ConnDb.getStatement(connection);
        ResultSet resultSet=null;

        try {
            resultSet=statement.executeQuery(str);
            while (resultSet.next()){
                Admin landlord1=new Admin();
                landlord1.setId(resultSet.getInt("id"));
                landlord1.setName(resultSet.getString("name"));
                landlord1.setSex(resultSet.getString("sex"));
                landlord1.setPhone(resultSet.getString("phone"));
                landlord1.setNumberid(resultSet.getString("numberid"));
                landlord1.setLoginname(resultSet.getString("loginname"));
                landlord1.setPassword(resultSet.getString("password"));

                list.add(landlord1);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }

        return list;
    }
}
